#pragma once

#include <iostream>
#include <string>
#include "Log.hpp"
#include "InetAddr.hpp"
#include "Common.hpp"

namespace SocketModule
{
    using namespace LogModule;
    const static int gbacklog = 16;
    class Socket // 基类
    {
    public:
        virtual ~Socket() {};
        virtual void SocketOrDie() = 0;
        virtual void BindOrDie(uint16_t port) = 0;
        virtual void ListenOrDie(int blacklog) = 0;
        virtual std::shared_ptr<Socket> Accept(InetAddr *client) = 0;
        virtual int Connect(std::string& server_ip,uint16_t server_port) = 0;
        virtual int Recv(std::string *out) = 0;
        virtual int Send(const std::string &message) = 0;
        virtual void Close() = 0;

    public:
        void BuiltTcpSocketMethod(uint16_t port, int backlog = gbacklog)
        {
            SocketOrDie();
            BindOrDie(port);
            ListenOrDie(backlog);
        }

        void BuildTcpClientSocketMethod()
        {
            SocketOrDie();
        }

        // void BuiltUdpSocketMethod(uint16_t port)
        // {
        //     SocketOrDie();
        //     BindOrDie(port);
        // }
    };

    const static int defaultfd = -1;
    class TcpSocket : public Socket
    {
    public:
        TcpSocket() : _sockfd(defaultfd)
        {
        }

        TcpSocket(int fd) : _sockfd(fd)
        {
        }

        ~TcpSocket() {}

        virtual void SocketOrDie() override
        {
            _sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
            if (_sockfd < 0)
            {
                LOG(LogLevel::FATAL) << "socket error";
                exit(SOCKET_ERR);
            }
            LOG(LogLevel::INFO) << "socket success";
        }

        virtual void BindOrDie(uint16_t port) override
        {
            InetAddr localaddr(port);
            int n = ::bind(_sockfd, localaddr.NetAddrPtr(), localaddr.NetAddrLen());
            if (n < 0)
            {
                LOG(LogLevel::FATAL) << "bind error";
                exit(BIND_ERR);
            }
            LOG(LogLevel::INFO) << "bind success";
        }

        virtual void ListenOrDie(int backlog) override
        {
            int n = ::listen(_sockfd, backlog);
            if (n < 0)
            {
                LOG(LogLevel::FATAL) << "listen error";
                exit(LISTEN_ERR);
            }
            LOG(LogLevel::INFO) << "listen success";
        }

        // accept用来接受来自客户端的连接请求的系统调用
        virtual std::shared_ptr<Socket> Accept(InetAddr *Client) override
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int fd = ::accept(_sockfd, CONV(peer), &len);
            if (fd < 0)
            {
                LOG(LogLevel::WARNING) << "accpet warning....";
                return nullptr;
            }
            Client->SetAddr(peer);
            // make_shared用来创建shared_ptr智能指针
            return std::make_shared<TcpSocket>(fd); // 父类指针指向派生类
        }

        int Recv(std::string *out) override // 把读到的数据带出去
        {
            char buffer[1024];
            ssize_t n = ::recv(_sockfd, buffer, sizeof(buffer) - 1, 0);
            if (n > 0)
            {
                buffer[n] = 0;
                *out += buffer;
            }
            return n;
        }

        int Send(const std::string &message) override
        {
            return send(_sockfd, message.c_str(), message.size(), 0);
        }

        int Connect(std::string& server_ip,uint16_t server_port) override
        {
            InetAddr server(server_ip,server_port);
            return ::connect(_sockfd,server.NetAddrPtr(),server.NetAddrLen());
        }

        void Close() override
        {
            if(_sockfd>=0)
            {
                ::close(_sockfd);
            }
        }

    private:
        int _sockfd;
    };

    // class UdpSocket :public Socket
    // {

    // };
}